import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_chat_ui/flutter_chat_ui.dart';
import 'package:flutter_chat_ui/src/models/preview_image.dart';

import '../public/Config.dart';

/// @class : image_gallery
/// @date : 2023-03-31 10:35
/// @name : LFC
/// @description :
/*
* 画廊效果
*缩放ViewPage
*
* */
class GalleryViewPage extends StatefulWidget {
  List<PreviewImage>? imgList;
  Function onClosePressed;
  GalleryViewPage(this.onClosePressed,{this.imgList});

  @override
  State<StatefulWidget> createState() => _GalleryViewPageState();
}


class _GalleryViewPageState extends State<GalleryViewPage> {
  List<PreviewImage> imgList = [];
  PageController? _pageController;

  var _currPageValue = 0.0;

  @override
  void initState() {
    super.initState();
    _pageController = PageController(viewportFraction: 0.85);
    _pageController?.addListener(() {
      setState(() {
        _currPageValue = _pageController?.page ?? 0;
      });
    });
  }

  @override
  void dispose() {
    super.dispose();
    _pageController?.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        widget.onClosePressed;
        return true;
      },
      child: Container(
        color: Colors.red,
        child: ImageGallery(
          images: imgList,
          pageController: _pageController!,
          onClosePressed: (){
            widget.onClosePressed;
            _pageController?.dispose();
            _pageController = null;
          },
        ),
      ),
    );
  }

}

